package com.baizhi.realmDemo;

import com.baizhi.dao.AdminDao;
import com.baizhi.entrty.Admin;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAccount;
import org.apache.shiro.realm.AuthenticatingRealm;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;

//继承AuthenticatingRealm
public class RealmDemo extends AuthenticatingRealm {
    @Autowired
    private AdminDao ad;
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        //1 从token中获取账号
        String username = (String) token.getPrincipal();
       /* //2 使用该账号 连接数据库 获取对应的信息
        //模拟连接数据库
        Admin admin = new Admin();
        admin.setUsername("xiaohei@qq.com");
        admin.setPassword("123456");*/
        //2 使用该账号 连接数据库 获取对应的信息

        Admin login = ad.loginUsername(username);
        //3创建一个info 封装数据库查询出来的数据
        //如果账号不存在返回null 就会抛出异常
        if(login!=null){
            SimpleAccount simpleAccount = new SimpleAccount(login.getUsername(), login.getPassword(), ByteSource.Util.bytes(login.getSalt()), this.getName());
            return simpleAccount;
        }
            return null;
    }
}
